﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using saigonbook.Biz;

namespace saigonbook.Dal
{
    public class DAL_phieunhap
    {
        public static List<BIZ_phieunhap> Select_phieunhap()
        {
            DataProvider db = new DataProvider();
            DataTable dt = db.GetData("Select * from PHIEUNHAP");
            List<BIZ_phieunhap> list = new List<BIZ_phieunhap>();
            foreach (DataRow row in dt.Rows)
            {
                BIZ_phieunhap phieunhap = new BIZ_phieunhap((int)row[0], row[1].ToString(),
                            row[2].ToString(), row[3].ToString(), row[4].ToString(), (DateTime)row[5], (int)row[6]);
                list.Add(phieunhap);
            }
            return list;
        }

        public static void Them_phieunhap(BIZ_phieunhap dto)
        {
            DataProvider db = new DataProvider();
            db.ExecuteQuery("Insert into phieunhap(maphieunhap,nhacungcap,diachi,sodienthoai,email,ngaynhap,tongtien) VALUES (N'" + dto.maphieunhap + "',N'" + dto.nhacungcap + "',N'" + dto.diachi + "',N'" + dto.sodienthoai + "',N'" + dto.email + "',N'" + dto.ngaynhap.Date.ToString("MM-dd-yyyy") + "',N'" + dto.tongtien +"')");
        }

        public static void Them_chitietphieunhap(BIZ_chitietphieunhap dto)
        {
            DataProvider db = new DataProvider();
            db.ExecuteQuery("Insert into chitietphieunhap(maphieunhap,masach,gia,soluong) VALUES (N'" + dto.phieunhap.maphieunhap + "',N'" + dto.sach.masach + "',N'" + dto.gia + "',N'" + dto.soluong +"')");
        }

        public static List<BIZ_chitietphieunhap> Select_chitietphieunhap(int maphieunhap)
        {
            DataProvider db = new DataProvider();
            DataTable dt = db.GetData("Select * from CHITIETPHIEUNHAP where maphieunhap = N'"+maphieunhap+"'");
            List<BIZ_chitietphieunhap> list = new List<BIZ_chitietphieunhap>();
            foreach (DataRow row in dt.Rows)
            {
                BIZ_chitietphieunhap ctpn = new BIZ_chitietphieunhap(DAL_phieunhap.GetPhieuNhapById((int)row[0]), 
                            DAL_sach.GetSachById((int)row[1]), (int)row[2], (int)row[3]);
                list.Add(ctpn);
            }
            return list;
        }

        public static List<BIZ_chitietphieunhap> Select_chitietphieunhap()
        {
            DataProvider db = new DataProvider();
            DataTable dt = db.GetData("Select * from CHITIETPHIEUNHAP");
            List<BIZ_chitietphieunhap> list = new List<BIZ_chitietphieunhap>();
            foreach (DataRow row in dt.Rows)
            {
                BIZ_chitietphieunhap ctpn = new BIZ_chitietphieunhap(DAL_phieunhap.GetPhieuNhapById((int)row[0]),
                            DAL_sach.GetSachById((int)row[1]), (int)row[2], (int)row[3]);
                list.Add(ctpn);
            }
            return list;
        }

        public static List<BIZ_chitietphieunhap> Select_chitietphieunhap(BIZ_sach sach)
        {
            DataProvider db = new DataProvider();
            DataTable dt = db.GetData("Select * from CHITIETPHIEUNHAP where masach=N'"+ sach.masach +"'");
            List<BIZ_chitietphieunhap> list = new List<BIZ_chitietphieunhap>();
            foreach (DataRow row in dt.Rows)
            {
                BIZ_chitietphieunhap ctpn = new BIZ_chitietphieunhap(DAL_phieunhap.GetPhieuNhapById((int)row[0]),
                            DAL_sach.GetSachById((int)row[1]), (int)row[2], (int)row[3]);
                list.Add(ctpn);
            }
            return list;
        }

        private static BIZ_phieunhap GetPhieuNhapById(int maphieunhap)
        {
            DataProvider db = new DataProvider();
            DataTable dt = db.GetData("Select * from phieunhap where maphieunhap='" + maphieunhap + "'");
            BIZ_phieunhap result = null;
            foreach (DataRow row in dt.Rows)
            {
                result = new BIZ_phieunhap((int)row[0], row[1].ToString(),
                            row[2].ToString(), row[3].ToString(), row[4].ToString(), (DateTime)row[5], (int)row[6]);
            }
            return result;
        }
    }
}
